--[[
Author: your name
Date: 2020-12-02 20:32:07
LastEditTime: 2020-12-04 11:42:40
LastEditors: your name
Description: In User Settings Edit
FilePath: \Lua\View\UICost\UIDailyGift.lua
--]]
UIDailyGift = Class("UIDailyGift", UIComponentBase)

function UIDailyGift:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent

    self.banner = self:GetChild("banner")
    self.titlePic = self:GetChild("titlePic")
    self.countDown = self:GetChild("time")
    self.giftList = self:SetDynamicList(self:GetChild("giftList"),self.updateItem)
    self.ctrlItem = CtrlManager.GetCtrl(PanelNames.Item);
end

function UIDailyGift:Refresh()
    logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.severData, self.itemTableData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    self.banner.url = "ui://UIDailyGift/"..self.modelTableData.f_Banner;
    self.titlePic.url = "ui://UIDailyGift/"..self.modelTableData.f_Tips;
    self.countDownNum = self.severData.endTime - HelperFunc.GetServerTime();
    self:RemoveTimeCall(self.OnCountDown);
    if self.countDownNum <= 0 then
        self.countDown.text = "活动已结束";
    else
        self:TimeCall(self.OnCountDown, 1000, -1);
        self.countDown.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
    end
    self.giftList.numItems = #self.severData.items;
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
end

function UIDailyGift:updateItem(index, itemObj)
    local serverData = self.severData.items[index];
    local tableData = self.itemTableData[tostring(serverData.subId)];
    if tableData == nil then
        itemObj.visible = false;
        return ;
    end
    itemObj.visible = true;
    local icon = itemObj:GetChild("icon");
    local title = itemObj:GetChild("title");
    local buy = itemObj:GetChild("buy");
    local buyNum = itemObj:GetChild("buyNum");
    local itemList = itemObj:GetChild("itemList");
    local typeController = itemObj:GetController("isSellOut");
    title.text = getLanguage(tableData.f_title, nil, "");
    if serverData.residue <= 0 then
        typeController.selectedIndex = 1;
    else
        typeController.selectedIndex = 0;
    end
    buyNum.text = string.format(getLanguage(self.modelTableData.f_Residue, nil, "今日可购：%d"), serverData.residue);
    icon.url = "ui://UIDailyGift/"..tableData.f_IllustrationPic;
    if tableData.f_Price == 0 then
        buy.title = "免费";
    else
        buy.title = "￥"..tableData.f_Price;
    end
    buy.onClick:Clear();
    buy.onClick:Add(function() self:ClickBuy(serverData.subId, tableData.f_ProductId, 1, tableData.f_Price); end);
    logicMgr.CostManager.RefreshItemList(itemList, decodeJsonStrToTabel(tableData.f_DailyConsumptionReward), 4);
end

function UIDailyGift:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self.countDown.text = "活动已结束";
        return true;
    end
    self.countDown.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
end

function UIDailyGift:ClickBuy(id, payId, num, money)
    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, id, payId, num, money, self.onBuySuccess, self);
end

function UIDailyGift:onBuySuccess()
    --UISysTips.AddMsg("购买成功");
end